package me.zk.beibao.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.AuthorizationScopeBuilder;
import springfox.documentation.builders.ImplicitGrantBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

import static springfox.documentation.builders.PathSelectors.ant;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @Author: zhang kai
 * @Date: 2021/09/11
 * @Description: Swagger 配置
 */
@EnableSwagger2
@Configuration
public class SpringFoxConfig {
    @Bean
    public Docket petApi(SecurityContext securityContext,SecurityScheme oauth) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("user-api")
                .apiInfo(userApiInfo())
                .select()
                .paths(regex(".*/user/api/.*"))
                .build()
                .securitySchemes(Collections.singletonList(oauth))
                .securityContexts(Collections.singletonList(securityContext));
    }





    private ApiInfo userApiInfo() {
        return new ApiInfoBuilder()
                .title("'背包'，用户模块API")
                .description("用户模块API接口文档")
                .termsOfServiceUrl("http://springfox.io")
                .contact(new Contact("springfox", "", "1210598@qq.com"))
                .license("Apache License Version 2.0")
                .licenseUrl("https://gitee.com/lovenile/beibao")
                .version("1.0")
                .build();
    }

    @Bean
    SecurityContext securityContext() {
        AuthorizationScope readScope = new AuthorizationScope("read:pets", "read your pets");
        AuthorizationScope[] scopes = new AuthorizationScope[1];
        scopes[0] = readScope;
        SecurityReference securityReference = SecurityReference.builder()
                .reference("petstore_auth")
                .scopes(scopes)
                .build();

        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(securityReference))
                .forPaths(ant(".*/api/user.*"))
                .build();
    }

    @Bean
    SecurityScheme oauth() {
        return new OAuthBuilder()
                .name("petstore_auth")
                .grantTypes(grantTypes())
                .scopes(scopes())
                .build();
    }

    @Bean
    SecurityScheme apiKey() {
        return new ApiKey("api_key", "api_key", "header");
    }

    List<AuthorizationScope> scopes() {
        return Arrays.asList(
                new AuthorizationScope("write:pets", "modify pets in your account"),
                new AuthorizationScope("read:pets", "read your pets"));
    }

    List<GrantType> grantTypes() {
        GrantType grantType = new ImplicitGrantBuilder()
                .loginEndpoint(new LoginEndpoint("http://petstore.swagger.io/api/oauth/dialog"))
                .build();
        return Collections.singletonList(grantType);
    }

    @Bean
    public SecurityConfiguration securityInfo() {
        return SecurityConfigurationBuilder.builder()
                .clientId("abc")
                .clientSecret("123")
                .realm("pets")
                .appName("petstore")
                .scopeSeparator(",")
                .build();
    }
}
